﻿#region Using namespaces

using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Input;

using BOL.Apps.GeneNetworkBrowser;

#endregion

namespace BOL.Apps.GeneNetworkBrowser.ViewModels
{
    public sealed class GenesTreeViewItemViewModel : TreeViewItemViewModel
    {
        #region SearchTreeViewItemCommand

        //private class SearchTreeViewItemCommand : ICommand
        //{
        //    readonly GenesTreeViewItemViewModel _vmGenes;

        //    public SearchTreeViewItemCommand(GenesTreeViewItemViewModel vmGenes)
        //    {
        //        _vmGenes = vmGenes;
        //    }

        //    public bool CanExecute(object parameter)
        //    {
        //        return true;
        //    }

        //    event EventHandler ICommand.CanExecuteChanged
        //    {
        //        add { }
        //        remove { }
        //    }

        //    public void Execute(object parameter)
        //    {
        //        _vmGenes.PerformSearch();
        //    }
        //}

        #endregion

        #region TreeViewItemViewModel implementation

        #region Private variables

        //private string _searchText;
        //private readonly ICommand _searchCommand;
        //private IEnumerator<GeneTreeViewItemViewModel> _matchingGenesEnumerator;

        #endregion

        #region Public properties

        ///// <summary>Returns the command used to execute a search in the tree.</summary>
        //public ICommand SearchCommand { get { return _searchCommand; } }

        ///// <summary>Gets or sets a fragment of the name to search for.</summary>
        //public string SearchText
        //{
        //    get { return _searchText; }
        //    set
        //    {
        //        if (value == _searchText)
        //            return;

        //        _searchText = value;
        //        _matchingGenesEnumerator = null;
        //    }
        //}

        public ObservableCollection<Gene> Genes { get; private set; }

        #endregion
        
        public GenesTreeViewItemViewModel(ObservableCollection<Gene> genes, NetworkTreeViewItemViewModel tvmNetwork)
            : base(tvmNetwork, true)
        {
            Genes = genes;
            _title = String.Format("Genes", genes.Count);
            //_searchCommand = new SearchTreeViewItemCommand(this);
        }

        protected override void LoadChildren()
        {
            foreach (var gene in Genes)
                base.Children.Add(new GeneTreeViewItemViewModel(gene, this));
        }

        //void PerformSearch()
        //{
        //    if (_matchingGenesEnumerator != null || !_matchingGenesEnumerator.MoveNext())
        //        VerifyMatchingItemEnumerator(_searchText);

        //    var item = _matchingGenesEnumerator.Current;

        //    if (item == null)
        //        return;

        //    // Ensure that this person is in view.
        //    if (item.Parent != null)
        //        item.Parent.IsExpanded = true;

        //    item.IsSelected = true;
        //}

        //void VerifyMatchingItemEnumerator(string searchText)
        //{
        //    var matches = FindMatches(searchText, this);
        //    _matchingGenesEnumerator = matches.GetEnumerator();

        //    if (!_matchingGenesEnumerator.MoveNext())
        //    {
        //        MessageBox.Show(
        //            "No matching names were found.",
        //            "Try again",
        //            MessageBoxButton.OK,
        //            MessageBoxImage.Information
        //            );
        //    }
        //}

        //IEnumerable<TreeViewItemViewModel> FindMatches(GenesTreeViewItemViewModel vmGenes)
        //{
        //    if (vmGenes.ContainsText(_searchText))
        //        yield return vmGenes;

        //    foreach (var child in vmGenes.Children)
        //        foreach (var match in FindMatches(child))
        //            yield return match;
        //}

        #endregion
    }
}
